package cn.northsea.service.impl;

import cn.northsea.mapper.PurchaseMapper;
import cn.northsea.pojo.*;
import cn.northsea.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public List<PurchaseVO> like(String purchaseOrder, String start, String end) {
        return purchaseMapper.like(purchaseOrder,start,end);
    }

    @Override
    public void statusPur(Integer id) {
        purchaseMapper.statusPur(id);
    }

    @Override
    public List<PurchaseTwoVO> findPurchase(String purchaseOrder, String start, String end) {
        return purchaseMapper.findPurchase(purchaseOrder,start,end);
    }

    @Override
    public  List<Supplier> findSupplier() {
        return purchaseMapper.findSupplier();
    }

    @Override
    public Supplier findName(String supplierName) {
        return purchaseMapper.findName(supplierName);
    }

    @Override
    public List<Material> findMaterials() {
        return purchaseMapper.findMaterials();
    }

    @Override
    public void addPurchase(Integer supplierId, Integer materialId, Integer quantity,Integer userId) {
        Date time = new Date(System.currentTimeMillis());
        Date hase = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = df.format(hase);
        sdf.format(time);
        Material material =purchaseMapper.getPrice(materialId);
        Double materialPrice = material.getMaterialPrice();
        Double total = materialPrice * quantity;
        purchaseMapper.addPurchase(supplierId,str,quantity,total,userId,time,materialId);
    }

    @Override
    public List<User> findUser() {
        return purchaseMapper.findUser();
    }

    @Override
    public String addCaiGou(Integer purchaseId, Integer materialId) {
        Material material = purchaseMapper.findMaterialsById(materialId);
        ErpSupplyOrder erpSupplyOrder = purchaseMapper.findpurchaseIdById(purchaseId);
        String order = purchaseMapper.getPurchaseOrder(erpSupplyOrder.getPurchaseOrder());
        if (order == null) {
            purchaseMapper.addCaiGou(purchaseId, materialId, erpSupplyOrder.getPurchaseQuantity(), material.getMaterialPrice(), erpSupplyOrder.getTotal(), erpSupplyOrder.getPurchaseOrder(), erpSupplyOrder.getOrderDate());
            return "ok";
        }
        return "false";
    }

    @Override
    public String delPurchase(Integer purchaseId) {
        ErpSupplyOrder erpSupplyOrder =purchaseMapper.findpurchaseIdById(purchaseId);
        if(erpSupplyOrder.getPurchaseOrder()!=null){
            purchaseMapper.delPurchase(purchaseId);
            purchaseMapper.delOrderItems(purchaseId);
            return "ok";
        }
        return "false";
    }




    @Override
    public ErpSupplyOrder findpurchaseIdById(Integer purchaseId) {
        return purchaseMapper.findpurchaseIdById(purchaseId);
    }

    @Override
    public void updatePurchase(Integer purchaseId,Integer supplierId, Integer materialId, Integer quantity, Integer userId) {
        purchaseMapper.updatePurchase(purchaseId,supplierId,materialId,quantity,userId);
    }
}
